package cn.jiahua.hospital.service.impl;

import cn.jiahua.hospital.dto.DoctorScheduleDto;
import cn.jiahua.hospital.dto.ScheduleDto;
import cn.jiahua.hospital.mapper.ScheduleMapper;
import cn.jiahua.hospital.pojo.Schedule;
import cn.jiahua.hospital.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.constants.Constants;

import javax.annotation.Resource;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Resource
    private ScheduleMapper scheduleMapper;

    @Override
    public Schedule getScheduleById(Integer id) {
        return scheduleMapper.getScheduleById(id);
    }

    @Override
    public List<Schedule> getScheduleListByMap(Map<String, Object> param) {
        return scheduleMapper.getScheduleListByMap(param);
    }

    @Override
    public List<ScheduleDto> getScheduleListByMap_hospital(Map<String, Object> param) {
        return scheduleMapper.getScheduleListByMap_hospital(param);
    }

    @Override
    public Integer getScheduleCountByMap_hospital(Map<String, Object> param) {
        return scheduleMapper.getScheduleCountByMap_hospital(param);
    }

    @Override
    public Integer getScheduleCountByMap(Map<String, Object> param) {
        return scheduleMapper.getScheduleCountByMap(param);
    }

    @Override
    public Integer qdtxAddSchedule(Schedule schedule) {
        return scheduleMapper.insertSchedule(schedule);
    }

    @Override
    public Integer qdtxModifySchedule(Schedule schedule) {
        return scheduleMapper.updateSchedule(schedule);
    }

    @Override
    public Integer updateSchedule_hospital(Schedule schedule) {
        return scheduleMapper.updateSchedule_hospital(schedule);
    }

    @Override
    public Integer qdtxDeleteScheduleById(Integer id) {
        return scheduleMapper.deleteScheduleById(id);
    }

    @Override
    public Integer qdtxBatchDeleteSchedule(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return scheduleMapper.batchDeleteSchedule(param);
    }

    @Override
    public Page<Schedule> querySchedulePageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = scheduleMapper.getScheduleCountByMap(param);
        pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Schedule> scheduleList = scheduleMapper.getScheduleListByMap(param);
        page.setList(scheduleList);
        return page;
    }

    @Override
    public List<DoctorScheduleDto> getDoctorSchedule(Integer doctorId) throws Exception {
        List<DoctorScheduleDto> doctorScheduleDtos = new ArrayList<DoctorScheduleDto>();
        //获取至今日起的一个星期的日期信息，将这些信息封装到dto中
        Date date = new Date();

        //封装今天的日期信息，当天的日期信息中没有预约号
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        String nowDate = formatter.format(date);
        DoctorScheduleDto doctorScheduleDto = new DoctorScheduleDto();
        doctorScheduleDto.setDate(nowDate);
        doctorScheduleDto.setWeek(date.getDay());
        doctorScheduleDtos.add(doctorScheduleDto);
        //设置后面几天的日期信息
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        //设置今天起后面七天的日期信息
        for (int i=1;i<=7;i++){
            DoctorScheduleDto doctorScheduleDto02 = new DoctorScheduleDto();
            //把日期往后增加一天.整数往后推,负数往前移动
            calendar.add(calendar.DATE,1);
            //这个时间就是日期往后推一天的结果
            date=calendar.getTime();
            String tomorrowStr = formatter.format(date);
            doctorScheduleDto02.setDate(tomorrowStr);
            doctorScheduleDto02.setWeek(date.getDay());

            doctorScheduleDtos.add(doctorScheduleDto02);
        }

        //根据doctorid获取排班表信息，并将排版信息封装到dto中
        Map<String,Object> map = new HashMap<>();
        map.put("doctorId",doctorId);
        List<Schedule> scheduleListByMap = scheduleMapper.getScheduleListByMap(map);
        if(scheduleListByMap==null||scheduleListByMap.size()<=0){
            return null;
        }
        Schedule schedule = scheduleListByMap.get(0);
        for (DoctorScheduleDto doctorScheduleDto02:doctorScheduleDtos){
            int week = doctorScheduleDto02.getWeek();
            if (doctorScheduleDto02.getDate().equals(nowDate)){
                continue;
            }
            switch (week){
                case 1:
                    doctorScheduleDto02.setAmNum(schedule.getAmOne());
                    doctorScheduleDto02.setPmNum(schedule.getPmOne());
                    break;
                case 2:
                    doctorScheduleDto02.setAmNum(schedule.getAmTwo());
                    doctorScheduleDto02.setPmNum(schedule.getPmTwo());
                    break;
                case 3:
                    doctorScheduleDto02.setAmNum(schedule.getAmThree());
                    doctorScheduleDto02.setPmNum(schedule.getPmThree());
                    break;
                case 4:
                    doctorScheduleDto02.setAmNum(schedule.getAmFour());
                    doctorScheduleDto02.setPmNum(schedule.getPmFour());
                    break;
                case 5:
                    doctorScheduleDto02.setAmNum(schedule.getAmFive());
                    doctorScheduleDto02.setPmNum(schedule.getPmFive());
                    break;
                case 6:
                    doctorScheduleDto02.setAmNum(schedule.getAmSix());
                    doctorScheduleDto02.setPmNum(schedule.getPmSix());
                    break;
                case 0:
                    doctorScheduleDto02.setAmNum(schedule.getAmSeven());
                    doctorScheduleDto02.setPmNum(schedule.getPmSeven());
                    break;
            }
        }
        return doctorScheduleDtos;
    }

    @Override
    public Integer updateSchedule_doctorId(Schedule schedule) {
        return scheduleMapper.updateSchedule_doctorId(schedule);
    }
}